package com.example.goodpet.my.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.goodpet.R;
import com.example.goodpet.base.BaseFragmentForView;
import com.example.goodpet.common.LoadingPage;
import com.example.goodpet.my.adapter.MyReleasePetAdapter;
import com.example.goodpet.bean.PetInformation;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.common.EndlessRecyclerOnScrollListener;
import com.example.goodpet.common.LoadMoreWrapper;
import com.example.goodpet.service.UserService;
import com.example.goodpet.utils.MyApplication;
import com.example.goodpet.utils.RetrofitSingleton;
import com.example.goodpet.utils.UIUtils;
import com.luck.picture.lib.decoration.WrapContentLinearLayoutManager;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MyPetInfoFragment extends BaseFragmentForView {
    private RecyclerView recyclerView;
    private List<PetInformation> data;
    private LoadMoreWrapper loadMoreWrapper;

    private Retrofit build;
    private UserService userService;

    @Override
    protected void LoadPage() {
        loadingPage.setStateCurrent(LoadingPage.STATE_loading);
        userService.getMyRelease(MyApplication.user.getId(),1).enqueue(new Callback<ResponseBody<List<PetInformation>>>() {
            @Override
            public void onResponse(Call<ResponseBody<List<PetInformation>>> call, Response<ResponseBody<List<PetInformation>>> response) {
                ResponseBody<List<PetInformation>> body = response.body();
                if (body!=null) {
                    loadingPage.setStateCurrent(LoadingPage.STATE_SUCCESS);
                    int code = body.getCode();
                    if (code == 200) {
                        //成功
                        data = body.getData();
                        MyReleasePetAdapter myReleaseAdapter = new MyReleasePetAdapter(data, getActivity(),MyPetInfoFragment.this);
                        loadMoreWrapper = new LoadMoreWrapper(myReleaseAdapter);
                        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
                        recyclerView.setAdapter(loadMoreWrapper);
                        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
                            @Override
                            public void onLoadMore() {
                                loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING);
                                userService.getMyRelease(MyApplication.user.getId(), ++loadMoreWrapper.page).enqueue(new Callback<ResponseBody<List<PetInformation>>>() {
                                    @Override
                                    public void onResponse(Call<ResponseBody<List<PetInformation>>> call, Response<ResponseBody<List<PetInformation>>> response) {
                                        ResponseBody<List<PetInformation>> body1 = response.body();
                                        //Log.d(TAG, "onResponse: "+ body);
                                        if (body1.getCode() == 200) {
                                            loadingPage.setStateCurrent(LoadingPage.STATE_SUCCESS);
                                            List<PetInformation> data1 = body1.getData();
                                            data.addAll(data1);
                                            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_COMPLETE);
                                        } else if (body1.getCode() == 404) {
                                            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_END);
                                        }

                                    }

                                    @Override
                                    public void onFailure(Call<ResponseBody<List<PetInformation>>> call, Throwable t) {
                                        UIUtils.toast("连接服务器失败", false);
                                    }
                                });
                            }
                        });
                    } else if (code == 404) {
                        loadingPage.setStateCurrent(LoadingPage.STATE_EMPTY);
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody<List<PetInformation>>> call, Throwable t) {
                loadingPage.setStateCurrent(LoadingPage.STATE_ERROR);
            }
        });
    }


    protected void initData() {
        build = RetrofitSingleton.getRetrofitNetConfig();
        userService = build.create(UserService.class);
    }

    @Override
    protected void initView(View inflate) {
        super.initView(inflate);
        recyclerView=inflate.findViewById(R.id.recyclerView);
    }

    @Override
    public void initListener() {
        super.initListener();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_user_info_rv;
    }

    public void  notifyDataList(int position){
        data.remove(position);
        loadMoreWrapper.notifyItemRemoved(position);
    }
}
